package com.chefu.order.controller;


import com.chefu.common.exception.pojo.PageResult;
import com.chefu.entity.dto.OrderDto;
import com.chefu.entity.pojo.Order;
import com.chefu.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 根据ID查询订单
     */
    @GetMapping("/order/{id}")
    public ResponseEntity<Order> findOrderById(@PathVariable("id") Long id) {
        Order order = orderService.findOrderById(id);
        return ResponseEntity.ok(order);
    }

    /**
     * 根据分页查询订单
     */
    @PostMapping("/order/page/{orderType}")
    public ResponseEntity<PageResult<Order>> orderPageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @PathVariable("orderType") String type,
            Order pageOrder
    ) {
        /*  PageResult<Order> pageResult = orderService.orderPageQuery(page, rows, startTime, endTime, pageOrder);*/
        PageResult<Order> pageResult = null;
        if (type.equals("1")) {
            /**
             * 查询服务
             */
            pageResult = orderService.orderPageQuery(page, rows, startTime, endTime, pageOrder,type);
        } else if (type.equals("2")) {
            /**
             * 查询配件
             */
            pageResult = orderService.orderPageQuery(page, rows, startTime, endTime, pageOrder,type);
        }

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据分页查询待处理订单
     */
    @PostMapping("/order/pagestatus/{orderStatus}")
    public ResponseEntity<PageResult<Order>> orderStatusPageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "startTime", required = false) Date startTime,
            @RequestParam(value = "endTime", required = false) Date endTime,
            @PathVariable("orderStatus")String Status,
            Order pageOrder
    ) {
        PageResult<Order> pageResult = orderService.orderStatusPageQuery(page, rows, startTime, endTime, pageOrder,Status);
        return ResponseEntity.ok(pageResult);
    }
    /**
     * 查看订单详情页面
     */
    @GetMapping("/order/detail/{id}")
    public ResponseEntity<OrderDto> finorderDetail(@PathVariable("id") Long id) {
        OrderDto orderDto = orderService.finorderDetailByid(id);
        return ResponseEntity.ok(orderDto);
    }
}
